package com.darker.secking.config;


import com.darker.secking.entity.User;
import com.darker.secking.service.IUserService;
import com.darker.secking.utils.CookieUtil;
import com.darker.secking.vo.RespBean;
import com.darker.secking.vo.RespBeanEnum;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

@Component
public class AccessInterceptor implements HandlerInterceptor {

    @Autowired
    IUserService userService;

    @Autowired
    RedisTemplate redisTemplate;





    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod){
            User user=getUser(request,response);
            UserContext.setUser(user);
            HandlerMethod hm= (HandlerMethod) handler;
            Accessimit accessimit = hm.getMethodAnnotation(Accessimit.class);
            if(accessimit==null){
                return true;
            }
            int maxCount = accessimit.maxCount();
            boolean needLogin = accessimit.needLogin();
            int second = accessimit.second();
            String key = request.getRequestURI();
            if(needLogin){
                if(user==null){
                    render(response, RespBeanEnum.SESSION_ERROR);
                    return false;
                }
                key+=":"+user.getId();
            }
            ValueOperations valueOperations = redisTemplate.opsForValue();
         Integer count= (Integer) valueOperations.get(key);
         if(count==null){
             valueOperations.set(key,1,second, TimeUnit.SECONDS);
         }else if(count<maxCount){
             valueOperations.increment(key);
         }
         else {
             render(response,RespBeanEnum.ACCESS_LIMIT_REACHED);
             return false;
         }

        }
        return true;
    }

    private void render(HttpServletResponse response, RespBeanEnum respBeanEnum) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        RespBean bean = RespBean.error(respBeanEnum);
        out.write(new ObjectMapper().writeValueAsString(bean));
        out.flush();
        out.close();
    }

    private User getUser(HttpServletRequest request, HttpServletResponse response) {
        String ticket= CookieUtil.getCookieValue(request,"userTicket");
        if(StringUtils.isEmpty(ticket)){
            return null;
        }
        return userService.getUserByCooike(ticket,request,response);

    }
}
